package conteoller;

import biz.CustomerBiz;
import entity.CustomertbEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.regex.Pattern;

/*
 * @Description锛�
 *       鐧诲綍澶勭悊servlet
 * @Author: JiangFan
 * @Date: 2019/3/8
 * @Time锛�16:08
 */
@WebServlet("/Logservlet")
public class Logservlet extends BaseServlet{
    CustomerBiz khb = new CustomerBiz ();

    public void login (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.setCharacterEncoding("utf-8");
    	PrintWriter pw=response.getWriter();
		Verification_code vc = new Verification_code();
		String userid = request.getParameter("userid");
		String pass = request.getParameter("pass");
		String Verification_code=request.getParameter("verification_code");
		List<CustomertbEntity> list =null;
		String number = vc.numb;
		if(pass==null&&Verification_code!=null) {
			list= khb.login(userid);
			if(list.size()==0) {
				if(khb.update(userid)>0) {
					request.setAttribute("Customer_xiaomi_id",list.get(0).getCustomer_xiaomi_id().toString());
					response.sendRedirect("../htmlwlz/zhuye.html");
				}
			}
			else {
				if(Verification_code==number) {
					request.setAttribute("Customer_xiaomi_id",list.get(0).getCustomer_xiaomi_id().toString());
					response.sendRedirect("../htmlwlz/zhuye.html");
				}
			}
		}
		else if(pass!=null&&userid.length()==11&&isInteger(userid)) {
			list= khb.login(userid);
			if(list.size()!=0) {
				if(list.get(0).getCustomer_password().toString().equals(pass)) {
					request.setAttribute("Customer_xiaomi_id",list.get(0).getCustomer_xiaomi_id().toString());
					response.sendRedirect("../htmlwlz/zhuye.html");
				}
				else {
					pw.write("<script language='javascript'>alert('密码错误')</script>");
					}
				}
			else {
				pw.write("<script language='javascript'>alert('未找到该用户，请注册')</script>");
			}
		}
		else if(pass!=null&&userid.length()==10&&isInteger(userid)) {
			list=khb.login_id(userid);
			if(list.size()!=0) {
				if(list.get(0).getCustomer_password().toString().equals(pass)) {
					request.setAttribute("Customer_xiaomi_id",list.get(0).getCustomer_xiaomi_id().toString());
					response.sendRedirect("../htmlwlz/zhuye.html");
				}
				else {
					pw.write("<script language='javascript'>alert('密码错误')</script>");
				}
			}
			else {
				pw.write("<script language='javascript'>alert('未找到该用户，请注册')</script>");
			}
		}
		else if(pass!=null&&userid.contains("@")) {
			list=khb.login_email(userid);
			if(list.size()!=0) {
				if(list.get(0).getCustomer_password().toString().equals(pass)) {
					request.setAttribute("Customer_xiaomi_id",list.get(0).getCustomer_xiaomi_id().toString());
					response.sendRedirect("../htmlwlz/zhuye.html");
				}
				else {
					pw.write("<script language='javascript'>alert('密码错误')</script>");
				}
			}
			else {
				pw.write("<script language='javascript'>alert('未找到该用户，请注册')</script>");
			}
		}
		else {
			pw.write("<script language='javascript'>alert('请输入正确的账号')</script>");
		}
	
	}
	public static boolean isInteger(String str) {
		Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
		return pattern.matcher(str).matches();
		}
}
